window.onload = initalizePage;
window.onunload = function() {}

var xhr = false;

function initalizePage() {
	var imgpara = document.createElement('p');
	imgpara.id = 'largeImage';
	var img = document.createElement('img');
	img.src =  '/24/only_2_m/images/full/' + document.location.hash.substring(1) + '.jpg';
	imgpara.appendChild(img);
	document.getElementById('content').appendChild(imgpara);
	var anchrs = document.getElementById('imagenavigator').getElementsByTagName('a');
	
	for (var i = 0; i < anchrs.length; i++) {
		anchrs[i].onclick = updateLargeImg;
	}
}

function updateLargeImg() {
	var img = document.getElementById('largeImage').getElementsByTagName('img')[0];
	img.src =  '/24/only_2_m/images/full/' + this.hash.substring(1) + '.jpg';
	document.location.hash = this.hash;
	return false;
}

function togglequicklogin(evt) {

	(evt) ? null : evt = window.event;

	xPos = evt.clientX;
	yPos = evt.clientY;

	document.getElementById("quickloginform") ? null : createquickloginform();

	var quickloginform = document.getElementById("quickloginform");

	(quickloginform.innerHTML == "") ? getquicklogin() : null;

	if (quickloginform.style.visibility == 'visible') {
		quickloginform.style.visibility = 'hidden';
	}
	else {
		quickloginform.style.left = (parseInt(xPos) - quickloginform.offsetWidth) + "px";
		quickloginform.style.top = parseInt(yPos)+4 + "px";
		quickloginform.style.visibility = 'visible';
		
	}

	return false;
}

function attachSubmitButton() {
	var loginFormSubmitButton = document.createElement("input");
	loginFormSubmitButton.id = "loginbutton";
	loginFormSubmitButton.name="Login";
	loginFormSubmitButton.type="submit";
	loginFormSubmitButton.value="Login";
	document.getElementById("quickloginform").appendChild(loginFormSubmitButton);
}

function createquickloginform() {
	var qloginform = document.createElement("form");
	qloginform.id = "quickloginform";
	qloginform.method = "POST"; //just incase ajax looses server conn, back up so can do normal post
	qloginform.action = "staff.php?action=authenticate";
	//qloginform.action = "staff.php?action=ajaxauthenticate";
	qloginform.onsubmit = ajaxauthenticate;
	//document.getElementById("loginbutton").onclick = function() {ajaxauthenticate(); return false;}
	document.getElementById("header").appendChild(qloginform);
}

//funny enuf created form doesnt seem to be able to rememebr passwords...need to look into!

function getquicklogin() {
	makexhr();

	var url = "frame/application/views/staff/_login_form.php";

	//hmmm...its ashame that i tried to hide frame..and tried to use it as server side includes only...
	//would it be reasonable to make a php file with include(to frame/views) on teh root dir, and have ajax url point to the non hidden php file..?

	if (xhr) {
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var outMsg = xhr.responseText;
				}
				else {
					outMsg = "Problem with the request " + xhr.status;
				}
		
				var quickloginform = document.getElementById("quickloginform");
		
				quickloginform.innerHTML = outMsg;
				attachSubmitButton()
				}
			}
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, couldn't create an XMLHttpRequest");
	}
}


function makexhr() {
/*
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
*/
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				try {
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e) {
				}
			}
		}
	}
/*
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			xhr = new XMLHttpRequest();
		} catch(e) {
			xhr = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
        		}
		}
	}
*/
}



function ajaxauthenticate() {
	makexhr();

	if (xhr) {
	
		var url = "?r=staff/process_ajax_login";
		var params = "username=" + encodeURI(document.getElementById("uname").value) + "&password=" + encodeURI(document.getElementById("pass").value);

		xhr.open("POST", url, true);

		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		//xhr.setRequestHeader("Content-length", params.length);
		//xhr.setRequestHeader("Connection", "close");
		

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && xhr.status == 200) {
				if (xhr.responseText != "false") {
					updatesafterloginsuccess();
				}
				else {
					updatesafterloginfail();
				}
				//document.getElementById("test").innerHTML = xhr.responseText;
			} else if (xhr.readyState == 1 || xhr.readyState == 2 || xhr.readyState == 3) {
			  createmessageareainqloginform();
			   document.getElementById("qloginnotice").innerHTML = "Authenticating...."; 
			}
		}
		xhr.send(params);
	}
	return false;
}


function updatesafterloginsuccess() {
        window.location = "?r=staff/menu";
        document.getElementById("notice").innerHTML = "You are now logged in.";
//      document.getElementById("quickloginform").style.visibility = 'hidden';
//	document.getElementById("header").removeChild(document.getElementById("quickloginform"));
//	document.getElementById("headerlogin").innerHTML = 'Logout';
//	document.getElementById("headerlogin").href = '?r=staff/logout';
//	var headeruname = document.createElement("a");
//	headeruname.id = "headeruname";
//	headeruname.innerHTML = xhr.responseText;
//	headeruname.href = '?r=staff/menu';
//	document.getElementById("header").insertBefore(headeruname, document.getElementById("logo"));
//	initalizePage();
}

function updatesafterloginfail() {
	if (!document.getElementById("qloginnotice")) {
	  createmessageareainqloginform();
	}
	document.getElementById("qloginnotice").innerHTML = "Invalid credentials.";
}

function createmessageareainqloginform() {
  var qloginnotice = document.createElement("div");
  qloginnotice.id = "qloginnotice";
  qloginnotice.style.color = 'red';
  document.getElementById("quickloginform").appendChild(qloginnotice);
}

function logmeout() {
  if (confirm("Are you sure you want to log out?")) {
        window.location = "?r=staff/logout"
	document.getElementById("notice").innerHTML = "logged out.";
        
  }
  return false;
/*  if (confirm("Are you sure you want to log out?")) {
    makexhr();
    var url = "?r=staff/logout";
    if (xhr) {
      xhr.onreadystatechange = function() {
	if (xhr.readyState == 4) {
	  if (xhr.status == 200) {
	    if ( xhr.responseText == "logoutsuccessfull" ) {
	      document.getElementById("header").removeChild(document.getElementById("headeruname"));
	      document.getElementById("headerlogin").innerHTML = 'Login';
	      document.getElementById("headerlogin").href = 'staff.php?action=login';
	      var outMsg = "You have successfully logged out";
	      initalizePage();
	    }
	    else {
	      outMsg = "Problem with logout, might already be logged out";
	    }
	  }
	  else {
	    outMsg = "Problem with the request " + xhr.status;
	  }
	}
	document.getElementById("notice").innerHTML = outMsg;
      }
      xhr.open("GET", url, true);
      xhr.send(null);
    }
    else {
      alert("Sorry, couldn't create an XMLHttpRequest");
    }
  }
  return false;
*/
}

